<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Str;

class apiEncryptMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //创建token
        $time = time();
        $nonce = Str::random(6);
        $token = createSign($time,$nonce);
        $data = $request->all();
        $data['time'] = $time;
        $data['nonce'] = $nonce;
        //加密数据
        $encrypt = apiEncrypt($data);
        //设置参数
        $request->replace(['data'=>$encrypt,'sign'=>$token]);
        return $next($request);
    }
}
